<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		<view class="buttons">
			<button @click="login">登录</button>
			<button @click="uploadImg">上传图片</button>
			<button @click="publish">新发布</button>
			<button @click="update">更新发布</button>
			<button @click="delte">删除发布</button>
			<button @click="updateUser">更新用户昵称</button>
			<button @click="getInfo">获取用户信息</button>
			<button @click="getAll">获取全部发布</button>
			<button @click="getMy">获取我的发布</button>
			<button @click="getPublishById">根据id查询发布详情</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	const publishObject = uniCloud.importObject('publish')
	const userObject = uniCloud.importObject('user')
	const uniId = uniCloud.importObject('uni-id-co')

	const title = ref('Hello')
	const imgUrls = ref([])
	const address = ref([])
	
	uni.getLocation({
		type: 'wgs84',
		success: function (res) {
			console.log('当前位置的经度：' + res.longitude);
			console.log('当前位置的纬度：' + res.latitude);
			address.value = [res.longitude, res.latitude]
		}
	});

	const login = () => {
		uni.login({
			success: (res) => {
				uniId.loginByWeixin({
					code: res.code
				})
			}
		})
	}

	function uploadImg() {
		uni.chooseImage({
			count: 1,
			success(res) {
				console.log(res);
				if (res.tempFilePaths.length > 0) {
					imgUrls.value = res.tempFilePaths[0]
				}
			}
		});
	}

	async function publish() {
		// const result = await uniCloud.uploadFile({
		// 	filePath: imgUrls.value,
		// 	cloudPath: imgUrls.value.replace(/.+tmp\//, ''),
		// 	onUploadProgress: function(progressEvent) {
		// 		console.log(progressEvent);
		// 		var percentCompleted = Math.round(
		// 			(progressEvent.loaded * 100) / progressEvent.total
		// 		);
		// 	}
		// });
		const result = {
			fileID: 'https://mp-9ebf33dd-23ba-4af0-9051-65fa02f47d3f.cdn.bspapp.com/cloudstorage/6d8b6089-887f-4ebf-8f25-5eee75d69f8f.jpg'
		}
		console.log(result);
		publishObject.add({
			content: '测试上传图片',
			imgUrls: [result.fileID],
			tag: 1,
			type: 2,
			price: 1000,
			address: address.value,
			addressDesc: '上海'
		})
	}
	
	async function update() {
		const result = {
			fileID: 'https://mp-9ebf33dd-23ba-4af0-9051-65fa02f47d3f.cdn.bspapp.com/cloudstorage/6d8b6089-887f-4ebf-8f25-5eee75d69f8f.jpg'
		}
		console.log(result);
		publishObject.update({
			content: '测试上传图片',
			imgUrls: [result.fileID],
			tag: 2,
			type: 3,
			price: 100,
			address: address.value,
			addressDesc: '上海',
			id: '643150d30c801c2ac9c7ae40'
		})
	}
	
	async function delte() {
		publishObject.delete({
			id: '643150d30c801c2ac9c7ae40'
		})
	}
	
	async function getAll() {
		const res = await publishObject.search({
			address: address.value,
			distance: 5 * 1000,
			tag: 1,
			type: 2
		})
		console.log(res);
	}
	
	async function getPublishById() {
		const res = await publishObject.getPublishById({
			id: '64325275f5cf3a3e0937a627'
		})
		console.log(res);
	}
	
	async function updateUser() {
		userObject.update({
			nickname: 'L.W.L'
		})
	}
	
	async function getInfo() {
		const res = await userObject.getInfo()
		console.log(res);
	}
	
	async function getMy() {
		const res = await publishObject.getMyPublish()
		console.log(res);
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	
	.buttons {
		display: flex;
		flex-wrap: wrap;
		flex-direction: flex-start;
	}
	
</style>